package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.cache.key.RedisKey;
import cn.wolfcode.luowowo.cache.service.IUserInfoCacheService;
import cn.wolfcode.luowowo.common.util.Consts;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

@Service
public class UserInfoCacheServiceImpl implements IUserInfoCacheService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    public void setUserInfoCache(String token, String json) {
        String cacheKey = RedisKey.USER_INFO.getCacheKey(token);
        //有效时间20分钟,时间单位毫秒
        redisTemplate.opsForValue().set(cacheKey,json, Consts.USER_INFO_TIMEOUT, TimeUnit.MILLISECONDS);

    }

    public String getUserInfoCache(String token) {
        String cacheKey = RedisKey.USER_INFO.getCacheKey(token);
        String json = redisTemplate.opsForValue().get(cacheKey);
        if(json!=null){
            //自动添加时间.
            redisTemplate.expire(cacheKey,Consts.USER_INFO_TIMEOUT, TimeUnit.MILLISECONDS);
        }
        return json;

    }
}
